#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e5 + 10, INF = 0x3f3f3f3f;

int n, m, mi = INF;
int c[N], ans[N];
pair<int, string> a[N];
vector<int> b;
map <string, vector<pair<int, int>>> hs;

void solve()
{
    cin >> n >> m;
    for (int i = 0;i < m;i++) {
        cin >> c[i];
        mi = min(mi, c[i]);
    }

    for (int i = 0;i < n;i++) {
        cin >> a[i].first >> a[i].second;
        a[i].first = -a[i].first;
        hs[a[i].second].push_back({ a[i].first,i });
    }
    for (auto& [_, v] : hs) {
        sort(v.begin(), v.end());
        for (int i = 0;i < mi && i < v.size();i++)  b.push_back(v[i].first);
    }
    b.push_back(1);
    sort(b.begin(), b.end());
    for (auto& [_, v] : hs) for (int i = 0;i < v.size();i++) {
        int w = v[i].first, id = v[i].second;
        int t = upper_bound(b.begin(), b.end(), w) - b.begin();
        ans[id] = t;
    }
    for (int i = 0;i < n;i++) cout << ans[i] << '\n';
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}